//
// Created by Administrator on 2021/11/11.
// May Saint Diana bless you!
//
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>

using namespace std;

class Solution {
public:
    static vector<string> subdomainVisits(vector<string> &cpdomains) {
        unordered_map<string, int> counter; // 统计每个域名的被访问次数
        for (auto &domain:cpdomains) {
            // 找到空格
            int spacePos = (int) domain.find(' ');
            // 找到次数
            int number = stoi(domain.substr(0, spacePos));
            // 解析剩下的域名
            for (int i = spacePos; i != domain.size(); ++i) {
                if (domain[i] == ' ' or domain[i] == '.') {
                    counter[domain.substr(i + 1)] += number;
                }
            }
        }
        // 整理答案
        vector<string> ans;
        ans.reserve(counter.size());
        for (auto &pair:counter) {
            ans.emplace_back(to_string(pair.second) + ' ' + pair.first);

        }
        return ans;
    }
};

int main() {
    vector<string> cpdomains{"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
    auto ans = Solution::subdomainVisits(cpdomains);
    for (const auto &x:ans) cout << x << endl;
    return 0;
}


